// pages/shangdian/Todos/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [], //记录数组
    value: '', //输入框文本
    paixu:false,//排序页面
    draggingIndex: null,
    startY: 0,
    offsetY: 0
  },
  //添加任务
  add(e) {
    let obj = {
      text: this.data.value,
      db: false
    }
    let list = this.data.list
    console.log(list);
    list.push(obj)
    this.setData({
      list: list,
      value: '',
    })
    wx.setStorageSync('todos', list)
  },
  //修改文字
  updateValue(e) {
    // console.log(e);
    if (e.detail.value.length >= 15) {
      wx.showToast({
        title: '只能输入十五个字',
        icon: 'none',
        duration: 1500
      })
      return
    }
    this.setData({
      value: e.detail.value
    })
  },
  //删除任务
  delete(e) {
    console.log(e.target.dataset.index);
    wx.showModal({
      title: '提示',
      content: '确定删除???',
      success: (res) => {
        if (res.confirm) {
          console.log('用户点击确定')
          let index = e.target.dataset.index
          let list = this.data.list
          list.splice(index, 1)
          this.setData({
            list: list,
          })
          wx.setStorageSync('todos', list)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  //确认完成
  queren(e) {
    let index = e.target.dataset.index
    let list = this.data.list
    list[index].db = !(list[index].db)
    this.setData({
      list: list,
    })
    wx.setStorageSync('todos', list)
  },
  //全部删除
  deleteAll() {
    wx.showModal({
      title: '提示',
      content: '全部???删除???',
      success: (res) => {
        if (res.confirm) {
          wx.setStorageSync('todos', [])
          this.setData({
            list: [],
          })
        } else if (res.cancel) {}
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let list = wx.getStorageSync('todos')
    if (!(list)) {
      list = []
    }
    console.log(list);
    this.setData({
      list: list
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log("下拉");
    wx.stopPullDownRefresh()
    this.onReady()
  },
 

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //排序
  paixu(){
    if(this.data.paixu){
      let list = this.data.list
      wx.setStorageSync('todos', list)
    }
    this.setData({
      paixu:!this.data.paixu
    })

  },

  //长按
  onLongPress(e) {
    // console.log("onLongPress");
    const index = e.target.dataset.index;
    // console.log(index);
    this.setData({
      draggingIndex: index,
      startY: e.touches[0].clientY
    });
  },
  //拖动
  onTouchMove(e) {
    // console.log(e);
    if (this.data.draggingIndex === null) return;
    const touch = e.touches[0];
    const offsetY = touch.clientY - this.data.startY;
    this.setData({
      offsetY
    });
  },
  //移动结束
  onTouchEnd() {
    // console.log("onTouchEnd");
    if (this.data.draggingIndex === null) return;
    const {
      list,
      draggingIndex,
      offsetY
    } = this.data;
    const itemHeight = 50; // 假设每个项高度为50px
    let newIndex //= draggingIndex + Math.round(offsetY / itemHeight);
    // 根据偏移量的正负选择合适的取整函数
    if (offsetY < 0) {
      // 向上拖动，使用 Math.ceil 向上取整
      newIndex = draggingIndex + (Math.ceil(offsetY / itemHeight)+1);
    } else {
      // 向下拖动，使用 Math.floor 向下取整
      newIndex = draggingIndex + Math.round((offsetY > 25 && offsetY < 50 ? 50 : offsetY) / itemHeight);
    }
    if (newIndex <= 0) {
      newIndex = 0
    }

    if (newIndex >= 0) {
      const newList = [...list];
      // console.log(newList);
      const [draggedItem] = newList.splice(draggingIndex, 1);
      newList.splice(newIndex, 0, draggedItem);

      this.setData({
        list: newList,
        draggingIndex: null,
        offsetY: 0
      });
    } else {
      this.setData({
        draggingIndex: null,
        offsetY: 0
      });
    }
  }














})